﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KCL.Code.Business;
using System.Data;

namespace KCL
{
    public partial class OneColumn : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[SessionManager.InSection] != null)
            {
                if ((int)Session[SessionManager.InSection] == 1)
                {
                    signin_submit.Text = "Thoát";
                    commandText.InnerText = string.Format("Xin chào {0} !", Session[SessionManager.Username].ToString());
                    lnkChangePassword.Visible = true;
                    password.Disabled = true;
                    username.Disabled = true;
                }
            }
        }

        protected void signin_submit_Click(object sender, EventArgs e)
        {
            DataTable userInfo = UserServices.CheckUserLogin(username.Value.Trim(), password.Value.Trim());
            if (userInfo.Rows.Count == 0 || userInfo == null)
            {
                Session.Add(SessionManager.InSection, 0);
                Response.Redirect("/");      
            }
            else
            {
                commandText.InnerText = string.Format("Xin chào {0} !", userInfo.Rows[0]["fullname"].ToString());
                password.Disabled = true;
                username.Disabled = true;
                lnkChangePassword.Visible = true;

                signin_submit.Text = "Thoát";

                Session.Add(SessionManager.InSection, 1);
                Session.Add(SessionManager.canCreate, userInfo.Rows[0]["create"]);
                Session.Add(SessionManager.canModify, userInfo.Rows[0]["edit"]);
                Session.Add(SessionManager.canRead, userInfo.Rows[0]["read"]);
                Session.Add(SessionManager.isAdmin, userInfo.Rows[0]["admin"]);
				Session.Add(SessionManager.UserId, userInfo.Rows[0]["id"]);
				Session.Add(SessionManager.Username, userInfo.Rows[0]["fullname"].ToString());
                Session.Add(SessionManager.UserId, userInfo.Rows[0]["id"].ToString());
            }
        } 
    }
}